package com.instagram.instadownloader.fragment.dialog;

import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;

import com.vine.vinedownloader.R;
import com.instagram.instadownloader.widget.PagerSlidingTabStrip;

// TODO: Auto-generated Javadoc
/**
 * The Class BasicStepsDialogFragment.
 */
public class BasicStepsDialogFragment extends DialogFragment {
	
	/** The tabs. */
	private PagerSlidingTabStrip tabs;
	
	/** The pager. */
	private ViewPager pager;
	
	/** The adapter. */
	private ContactPagerAdapter adapter;

	/**
	 * New instance.
	 *
	 * @return the basic steps dialog fragment
	 */
	public static BasicStepsDialogFragment newInstance() {
		BasicStepsDialogFragment f = new BasicStepsDialogFragment();
		return f;
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
	 */
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (getDialog() != null) {
			getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
			getDialog().getWindow().setBackgroundDrawableResource(
					android.R.color.transparent);
		}

		View root = inflater.inflate(R.layout.fragment_quick_guide, container,
				false);

		tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
		pager = (ViewPager) root.findViewById(R.id.pager);
		settingSlidingTabs();

		adapter = new ContactPagerAdapter();
		pager.setAdapter(adapter);
		tabs.setViewPager(pager);

		return root;
	}

	/**
	 * Setting sliding tabs.
	 */
	private void settingSlidingTabs() {
		tabs.setIndicatorColor(getResources().getColor(
				R.color.button_press_yellow));
		tabs.setTextColor(getResources().getColor(R.color.hami_green));
		tabs.setUnderlineHeight(getResources().getDimensionPixelSize(
				R.dimen.tab_underline_height));
		tabs.setUnderlineColorResource(R.color.button_press_yellow);
	}

	/* (non-Javadoc)
	 * @see android.support.v4.app.DialogFragment#onStart()
	 */
	@SuppressWarnings("deprecation")
	@Override
	public void onStart() {
		super.onStart();

		// change dialog width
		if (getDialog() != null) {

			int fullWidth = getDialog().getWindow().getAttributes().width;
			int fullHeight = getDialog().getWindow().getAttributes().height;
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
				Display display = getActivity().getWindowManager()
						.getDefaultDisplay();
				Point size = new Point();
				display.getSize(size);
				fullWidth = size.x;
				fullHeight = size.y;
			} else {
				Display display = getActivity().getWindowManager()
						.getDefaultDisplay();
				fullWidth = display.getWidth();
				fullHeight = display.getHeight();
			}

			final int padding = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
							.getDisplayMetrics());

			int w = fullWidth - padding;
			int h = fullHeight * 2 / 3 - padding;

			getDialog().getWindow().setLayout(w, h);
		}
	}

	/**
	 * The Class ContactPagerAdapter.
	 */
	public class ContactPagerAdapter extends PagerAdapter {

		/** The titles. */
		private final String[] TITLES = { "COPY SHARE URL", "FROM INSTAGRAM",
				"OPEN INSTADOWNLOADER", "PASTE IT", "CHECK SHARE URL",
				"FOUND RESOURCE", "NOW HAVE YOUR COLLECTION" };

		/** The image. */
		private final int[] IMAGE = { R.drawable.dr_002_copy_share_url,
				R.drawable.dr_003_wait_copy, R.drawable.dr_004_taskmanager,
				R.drawable.dr_005_paste, R.drawable.dr_008_wait_checking,
				R.drawable.dr_009_available_resource, R.drawable.dr_011_history };

		/**
		 * Instantiates a new contact pager adapter.
		 */
		public ContactPagerAdapter() {
			super();
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
		public int getCount() {
			return TITLES.length;
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getPageTitle(int)
		 */
		@Override
		public CharSequence getPageTitle(int position) {
			return TITLES[position];
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#instantiateItem(android.view.ViewGroup, int)
		 */
		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			// looks a little bit messy here
			ImageView v = new ImageView(getActivity());
			v.setImageResource(IMAGE[position]);
			final int padding = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 16, getResources()
							.getDisplayMetrics());
			v.setPadding(padding, padding, padding, padding);
			container.addView(v, 0);
			return v;
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#destroyItem(android.view.ViewGroup, int, java.lang.Object)
		 */
		@Override
		public void destroyItem(ViewGroup container, int position, Object view) {
			container.removeView((View) view);
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#isViewFromObject(android.view.View, java.lang.Object)
		 */
		@Override
		public boolean isViewFromObject(View v, Object o) {
			return v == ((View) o);
		}

	}
}
